package com.v1.video.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.activity.LiveVRPlayerActivity;
import com.v1.video.domain.VRLive;
import com.v1.video.util.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * 直播列表适配器
 * 
 * @author wangfang
 * 
 */
public class LiveListAdapter extends BaseAdapter {

	private Context mContext;
	private LayoutInflater mLayoutInflater;
	private int mHeight;
	private List<VRLive> mLstData;

	public LiveListAdapter(Context context) {
		this.mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mHeight = Utils.computeImageHeight_16_5(mContext);
		mLstData = new ArrayList<VRLive>();
	}

	public List<VRLive> getLstData() {
		return mLstData;
	}

	public void setLstData(List<VRLive> mLstData) {
		if (mLstData != null && mLstData.size() > 0) {
			this.mLstData = mLstData;
			this.notifyDataSetChanged();
		}
	}
	
	public void setAddLstData(List<VRLive> mLstData) {
		if (mLstData != null && mLstData.size() > 0) {
			this.mLstData.addAll(mLstData);
			this.notifyDataSetChanged();
		}
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mLstData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mLstData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		Holder holder = null;
		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.activity_livelist_item, null);
			holder = new Holder();
			holder.image = (ImageView) convertView.findViewById(R.id.iv_image);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		final VRLive info = mLstData.get(position);
		LayoutParams params = holder.image.getLayoutParams();
		params.height = mHeight;
		holder.image.setLayoutParams(params);
		if (!TextUtils.isEmpty(info.getPic())) {
			ImageLoader.getInstance().displayImage(info.getPic(), holder.image,
					Constant.VIDEO_10_16_OPTION);
		} else {
			holder.image
					.setImageResource(R.drawable.common_place_holder_bg_16_10);
		}
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(mContext, LiveVRPlayerActivity.class);
				intent.putExtra("videourl", info.getUrl());
				mContext.startActivity(intent);
			}
		});
		return convertView;
	}

	private class Holder {
		public ImageView image;
	}

}
